
app = {
    files: [
    'libs/classes.js',
    'libs/debug.js',
    'libs/system.js',
    'libs/tuenti.js',
    'modules/config.js',
    'modules/themes.js',
    'modules/background.js',
    'modules/ads.js',
    'modules/avatar.js',
    'modules/bbcode.js',
    'modules/invite.js',
    'modules/updater.js',
    'modules/enchancer.js',
    'modules/ghostimage.js',
    'modules/preview.js',
    'modules/stats.js'
    ],
    xmlhttpRequest: function (details) {
        function setupEvent(xhr, url, eventName, callback) {
            xhr[eventName] = function () {
                var isComplete = xhr.readyState == 4;
                var responseState = {
                    responseText: xhr.responseText,
                    readyState: xhr.readyState,
                    responseHeaders: isComplete ? xhr.getAllResponseHeaders() : "",
                    status: isComplete ? xhr.status : 0,
                    statusText: isComplete ? xhr.statusText : "",
                    finalUrl: isComplete ? url : ""
                };
                callback(responseState);
            };
        }

        var xhr = new XMLHttpRequest();
        var eventNames = ["onload", "onerror", "onreadystatechange"];
        for (var i = 0; i < eventNames.length; i++ ) {
            var eventName = eventNames[i];
            if (eventName in details) {
                setupEvent(xhr, details.url, eventName, details[eventName]);
            }
        }
        xhr.open(details.method, details.url);

        if (details.overrideMimeType) {
            xhr.overrideMimeType(details.overrideMimeType);
        }
        if (details.headers) {
            for (var header in details.headers) {
                xhr.setRequestHeader(header, details.headers[header]);
            }
        }
        xhr.send(details.data ? details.data : null);

    },

    make: function() {
        document.getElementById('input').value = '';
        this.parse('',0);
    },
    parse: function(script, index) {

        if (!(index < app.files.length)) {
            document.getElementById('input').value = script;
            return;
        }

        app.xmlhttpRequest({
            method: "GET",
            url: '../' + app.files[index],
            onload: function(response) {
                var packer = new Packer();
                script += packer.pack(response.responseText, false, false) + '\n';
                app.parse(script, index + 1);
            }
        });

    }
};